package com.bw.controller;

import com.bw.bean.DyingBean;
import com.bw.bean.FleiBean;
import com.bw.bean.Middle1Bean;
import com.bw.service.DyingBeanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.dc.pr.PRError;

import javax.annotation.Resource;
import javax.jws.WebParam;
import java.lang.reflect.Array;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class DyingBeanController {
    @Resource
    private DyingBeanService dyingBeanService;
    @RequestMapping("list")
    public String getlist(Model model, @RequestParam(defaultValue = "1")Integer pageNum,
                          @RequestParam(defaultValue = "")String sname){
        PageHelper.startPage(pageNum,3);
        Map map = new HashMap();
        map.put("sname",sname);
        List<DyingBean> list = dyingBeanService.list(map);
        PageInfo pageInfo = new PageInfo(list);
        System.out.println(list);
        model.addAttribute("plist",pageInfo);
        model.addAttribute("sname",sname);
        return "list";
    }

    @RequestMapping("del")
    @ResponseBody
    public int getdel(Integer[] sid){
        int i = dyingBeanService.delDying(sid);
        if(i>0){
            int i1 = dyingBeanService.delMiddle(sid);
            return i1;
        }
        return 0;
    }
    @RequestMapping("add")
    public String getadd(DyingBean dyingBean,Integer[] fl){
        int i = dyingBeanService.addDying(dyingBean);
        if (i>0){
            for (int s :fl){
                Middle1Bean middle1Bean = new Middle1Bean();
                middle1Bean.setSid(dyingBean.getSid());
                middle1Bean.setCid(s);
                int i1 = dyingBeanService.addMiddle(middle1Bean);
            }
        }
        return "redirect:list";
    }
    @RequestMapping("huixian")
    @ResponseBody
    public List<FleiBean> gethuixian(){
        List<FleiBean> huixian = dyingBeanService.huixian();
        return huixian;
    }
    @RequestMapping("idlist")
    @ResponseBody
    public DyingBean getidlist(Integer sid){
        DyingBean idlist = dyingBeanService.idlist(sid);
        return idlist;
    }

    @RequestMapping("update")
    public String getupdate(DyingBean dyingBean,Integer[] fl){
        int i = dyingBeanService.updateDying(dyingBean);
        Integer[] sid = {dyingBean.getSid()};
        int i1 = dyingBeanService.delMiddle(sid);
        if (i>0){
            for (int s :fl){
                Middle1Bean middle1Bean = new Middle1Bean();
                middle1Bean.setSid(dyingBean.getSid());
                middle1Bean.setCid(s);
                dyingBeanService.addMiddle(middle1Bean);
            }
        }
        return "redirect:list";
    }
}
